const { successData, serverError } = require('../utils/common')

const {
  createOrUpdate,
  findCarts,
  updateCarts,
  removeCarts,
  selectAllCarts,
  unselectAllCarts,
  countCarts,
} = require('../service/carts.service')

const {
  findCartError,
  cartFormatError,
  updateCartError,
  removeCartError,
} = require('../constant/error.type')
class CartController {
  async add(ctx) {
    // 添加到购物车
    const { goods_id } = ctx.request.body
    const { id: user_id } = ctx.state.user
    try {
      const res = await createOrUpdate(user_id, goods_id)
      ctx.body = successData('加入购物车成功', res)
    } catch (error) {
      console.error('加入购物车失败', error)
      return ctx.app.emit('error', addCartError, ctx)
    }
  }

  async findAll(ctx) {
    const { pageNum = 1, pageSize = 10 } = ctx.request.query
    const { id: user_id } = ctx.state.user
    try {
      // 2. 调用数据处理方法
      const res = await findCarts(pageNum, pageSize, user_id)
      // 3. 返回结果
      ctx.body = successData('获取购物车列表成功', res)
    } catch (error) {
      console.error('获取购物车列表失败', error)
      return ctx.app.emit('error', findCartError, ctx)
    }
  }

  async update(ctx) {
    const { id } = ctx.request.params
    const { number, selected } = ctx.request.body

    if (number === undefined && selected == undefined) {
      cartFormatError.message = 'number和selected不能同时为空'
      return ctx.app.emit('error', cartFormatError, ctx)
    }

    try {
      const res = await updateCarts({ id, number, selected })
      ctx.body = successData('更新购物车成功', res)
    } catch (error) {
      console.error('更新购物车失败', error)
      return ctx.app.emit('error', updateCartError, ctx)
    }
  }

  async remove(ctx) {
    const { ids } = ctx.request.body
    try {
      const res = await removeCarts(ids)
      ctx.body = successData('删除购物车商品成功', res)
    } catch (error) {
      console.error('删除购物车商品失败', error)
      return ctx.app.emit('error', removeCartError, ctx)
    }
  }

  async selectAll(ctx) {
    const { id: user_id } = ctx.state.user
    try {
      const res = await selectAllCarts(user_id)
      ctx.body = successData('全选操作成功', res)
    } catch (error) {
      console.error('全选操作失败', error)
      serverError.message = '全选操作失败'
      return ctx.app.emit('error', serverError, ctx)
    }
  }

  async unselectAll(ctx) {
    const { id: user_id } = ctx.state.user
    try {
      const res = await unselectAllCarts(user_id)
      ctx.body = successData('全不选操作成功', res)
    } catch (error) {
      console.error('全不选操作失败', error)
      serverError.message = '全不选操作失败'
      return ctx.app.emit('error', serverError, ctx)
    }
  }

  async countCart(ctx) {
    const { id: user_id } = ctx.state.user

    try {
      const res = await countCarts(user_id)
      ctx.body = successData('获取购物车内商品总数成功', res)
    } catch (error) {
      console.error('获取购物车内商品总数成功失败', error)
      serverError.message = '获取购物车内商品总数成功失败'
      return ctx.app.emit('error', serverError, ctx)
    }
  }
}

module.exports = new CartController()
